<?php
/**
 * PsiFramework
 *
 * Php Simple Framework
 *
 * PHP version 5.3
 *
 * @category  PsiFramework.Plugins.Shop.Classes.DAO
 * @package   PsiFramework.Plugins.Shop.Classes.DAO
 * @author    Remi San <remi.san@gmail.com>
 * @copyright 2011 Remi San
 * @license   http://creativecommons.org/licenses/by/3.0/  Creative Commons CC BY 3.0
 * @version   SVN: <svn_id>
 * @link      http://www.psiframework.com/phpdoc/classes/db_\PsiFramework\Plugins\Shop\Classes\DAO\OrderDAO.html
 */

namespace PsiFramework\Plugins\Shop\Classes\DAO;

import("Classes.DAO.AbstractDAO");
import("Classes.Util.Parameters.ObjectParameters");

use \PsiFramework\Classes\DAO\AbstractDAO;
use \PsiFramework\Classes\Model\Object;
use \PsiFramework\Classes\Util\Parameters\ObjectParameters;
use \PsiFramework\Classes\Util\Database\Filters\QueryFilter;
use \PsiFramework\Classes\Util\Database\Query\SqlColumn;
use \PsiFramework\Classes\Util\Database\Query\SqlFunction;
use \PsiFramework\Classes\Util\Database\Query\SqlOperation;
use \PsiFramework\Classes\Util\Database\Query\SqlTable;
use \PsiFramework\Classes\Util\Database\Query\SqlValue;


/**
 * The Order Data Access Object
 *
 * This is the default class for the Order Data Access Object
 *
 * @category PsiFramework.Plugins.Shop.Classes.DAO
 * @package  PsiFramework.Plugins.Shop.Classes.DAO
 * @author   Remi San <remi.san@gmail.com>
 * @license  http://creativecommons.org/licenses/by/3.0/  Creative Commons CC BY 3.0
 * @link     http://www.psiframework.com/phpdoc/classes/db_\PsiFramework\Plugins\Shop\Classes\DAO\OrderDAO.html
 */
class OrderDAO extends AbstractDAO
{
    /**
     * The singleton
     * 
     * @var OrderDAO
     */
    protected static     $_singleton = null;

    /**
     * OrderDAO constructor
     * 
     * @param ObjectParameters $objectParameters The object parameters
     * 
     * @return OrderDAO
     */
    protected function __construct(
        ObjectParameters $objectParameters=null
    )
    {
        parent::__construct($objectParameters);
    }

    /**
     * Retrieve all the orders for a user
     *
     * @param IUser $user          The user
     * @param bool  $checkSecurity Check security ?
     *
     * @return Order[]
     */
    public function retrieveOrdersByUser(
        /*IUser*/ $user,
        /*bool*/  $checkSecurity=true
    )
    {
        if (is_null($user) || is_null($user->getId())) {
        	return array();
        }
        
        $col = new SqlColumn('user_id');
    	$value = new SqlValue($user->getId());
    
    	$equal = new SqlOperation(SqlOperation::$equal);
    	$equal->addParameter($col);
    	$equal->addParameter($value);
    
    	return $this->retrieveAll(new QueryFilter($equal), null, $checkSecurity);
    }
    
    /**
     * Retrieve an active order by user
     *
     * @param IUser $user          The user
     * @param bool  $checkSecurity Check security ?
     *
     * @return Order
     */
    public function retrieveActiveOrderByUser(
        /*IUser*/ $user,
        /*bool*/  $checkSecurity=true
    )
    {
    	if (is_null($user) || is_null($user->getId())) {
    	    return null;
    	}
        
        $and = new SqlOperation(SqlOperation::$and);
        
        $col = new SqlColumn('user_id');
    	$value = new SqlValue($user->getId());
    
    	$equal = new SqlOperation(SqlOperation::$equal);
    	$equal->addParameter($col);
    	$equal->addParameter($value);
    	
    	$and->addParameter($equal);
    	
    	$colTwo = new SqlColumn('status');
    	
    	$equalTwo = new SqlOperation(SqlOperation::$isNull);
    	$equalTwo->addParameter($colTwo);
    	
    	$and->addParameter($equalTwo);
    
    	$orders = $this->retrieveAll(new QueryFilter($and), null, $checkSecurity);
    
    	if (!is_null($orders) && count($orders)>0) {
    		return $orders[0] ;
    	} else {
    		return null ;
    	}
    }
    
    /**
     * Retrieves the order with its number
     *
     * @param string  $number        The $number
     * @param boolean $checkSecurity Check the security ?
     *
     * @return Order
     */
    public function retrieveByNumber(/*string*/ $number, /*boolean*/ $checkSecurity=true)
    {
    	if (isNullOrEmptyString($number)) {
    		return null ;
    	}
    
    	$colNom = new SqlColumn("order_number");
    
    	$value = mb_strtoupper($number, 'utf-8');
    	$sqlValue = new SqlValue($value);
    
    	$upper = new SqlFunction("UPPER");
    	$upper->addParameter($colNom);
    
    	$op = new SqlOperation(SqlOperation::$equal);
    	$op->addParameter($upper);
    	$op->addParameter($sqlValue);
    
    	$result = $this->retrieveAll(new QueryFilter($op), null, $checkSecurity);
    
    	if (count($result)>0) {
    		return $result[0] ;
    	} else {
    		return null ;
    	}
    }
    
    /**
     * The function to call instead of the constructor.
     *
     *@param ObjectParameters $objectParameters the object parameters 
     *                                          for Order
     *
     * @return OrderDAO instance
     */
    public static function getInstance(ObjectParameters $objectParameters)
    {

        if (self::$_singleton==null) {
            self::$_singleton = new OrderDAO($objectParameters);
        }

        return self::$_singleton;
    }
}
